using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using KS.Gantt;
using KS.Gantt.Dialogs.Controls;

namespace MiGuava
{
    public partial class ucTasks : ucGridView
    {        
        public ucTasks()
        {
            InitializeComponent();

            GRD.AutoGenerateColumns = false;
            GRD.AllowUserToAddRows = false;
            GRD.AllowUserToResizeRows = false;

            GRD.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(GRD_EditingControlShowing);
            GRD.CellFormatting += new DataGridViewCellFormattingEventHandler(GRD_CellFormatting);
            GRD.CellBeginEdit += new DataGridViewCellCancelEventHandler(GRD_CellBeginEdit);
            GRD.CellEndEdit += new DataGridViewCellEventHandler(GRD_CellEndEdit);            
        }

        private DurationInputBox durationInputBox = null;
        void GRD_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DurationInputBoxEditingControl)
            {
                durationInputBox = GRD.EditingControl as DurationInputBox;
            }
        }


        void GRD_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (GRD.Columns[e.ColumnIndex].Name == "Duration" && e.RowIndex >= 0)
            {            
                TaskItem task = (TaskItem)GRD.Rows[e.RowIndex].DataBoundItem;

                DataGridViewDurationTextBoxCell dgc = (DataGridViewDurationTextBoxCell)GRD.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (dgc != null && task != null)
                {
                    dgc.DurationType = task.DurationType;
                    dgc.RoundToGranularity = false;
                    dgc.AutomaticDurationType = true;
                    dgc.GanttControl = m_GanttControl;
                }                
            }
        }

        void GRD_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                TaskItem task = (TaskItem)GRD.Rows[e.RowIndex].DataBoundItem;

                string ColumnName = GRD.Columns[e.ColumnIndex].Name;
                switch (ColumnName)
                {
                    case "StartDate":
                    case "EndDate":
                    case "Duration":
                        if (task.IsMilestone)
                            e.Cancel = true;                        

                        break;
                }
            }
        }

        void GRD_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {            
            if (CurrentTask != null)
            {
                if (durationInputBox != null)
                {
                    durationInputBox.AutomaticDurationType = true;                    
                    durationInputBox.Duration = durationInputBox.Duration;
                    CurrentTask.DurationType = durationInputBox.DurationType;
                    CurrentTask.Duration = durationInputBox.Duration;
                    durationInputBox = null;
                }

                switch (GRD.Columns[e.ColumnIndex].Name)
                {
                    case "EndDate":
                        CurrentTask.EndEdit(false, GanttItem.TaskShiftModes.fromRight, true);
                        break;

                    case "StartDate":
                        CurrentTask.EndEdit(false, GanttItem.TaskShiftModes.fromLeft, true);
                        break;

                    case "Duration":
                        CurrentTask.EndEdit(false, GanttItem.TaskShiftModes.fromLeft, true);
                        break;                    
                }
                
                CurrentTask = null;
            }
        }

        private TaskItem CurrentTask = null;
        protected override void GRD_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            base.GRD_CellValidating(sender, e);            

            if (GRD.IsCurrentCellDirty)
            {
                CurrentTask = (TaskItem)GRD.Rows[e.RowIndex].DataBoundItem;
                CurrentTask.BeginEdit();                
            }            
        }

        public override void InitData()
        {
            GRD.Columns.Clear();

            this.DataSource = m_GanttControl.Tasks;                        

            // SetUp custom edit controls            

            // Text
            DataGridViewTextBoxColumn txtText = new DataGridViewTextBoxColumn();
            txtText.Name = "Text";
            txtText.DataPropertyName = "Text";
            txtText.HeaderText = Properties.Resources.ColumnHeader_Text;
            txtText.Width = 240;
            GRD.Columns.Add(txtText);            

            // Priority
            DataGridViewComboBoxColumn colPriority = new DataGridViewComboBoxColumn();
            colPriority.Name = "Priority";
            KS.Gantt.Dialogs.ComboBoxHelpers.FillComboEx(colPriority, typeof(KS.Gantt.TaskItem.TaskPriorities));
            colPriority.DataPropertyName = "Priority";
            colPriority.HeaderText = Properties.Resources.ColumnHeader_Priority;
            colPriority.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            GRD.Columns.Add(colPriority);

            // Status
            DataGridViewComboBoxColumn colStatus = new DataGridViewComboBoxColumn();
            colStatus.Name = "Status";
            KS.Gantt.Dialogs.ComboBoxHelpers.FillComboEx(colStatus, typeof(KS.Gantt.TaskItem.TaskStates));
            colStatus.DataPropertyName = "Status";
            colStatus.HeaderText = Properties.Resources.ColumnHeader_Status;
            colStatus.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            GRD.Columns.Add(colStatus);            

            // PercentDone
            DataGridViewNumericUpDownColumn colPercentDone = new DataGridViewNumericUpDownColumn();
            colPercentDone.Name = "PercentDone";
            colPercentDone.DataPropertyName = "PercentDonePercent";
            colPercentDone.HeaderText = Properties.Resources.ColumnHeader_PercentDone;
            colPercentDone.SortMode = DataGridViewColumnSortMode.NotSortable;
            colPercentDone.ReadOnly = false;
            colPercentDone.Minimum = 0;
            colPercentDone.Maximum = 100;
            colPercentDone.DecimalPlaces = 0;
            colPercentDone.ThousandsSeparator = false;
            colPercentDone.Increment = 5;            
            colPercentDone.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            GRD.Columns.Add(colPercentDone);

            // IsMilestone
            DataGridViewCheckBoxColumn chkIsMilestone = new DataGridViewCheckBoxColumn();
            chkIsMilestone.Name = "IsMilestone";
            chkIsMilestone.DataPropertyName = "IsMilestone";
            chkIsMilestone.HeaderText = Properties.Resources.ColumnHeader_IsMilestone;
            GRD.Columns.Add(chkIsMilestone);

            // StartDate
            DataGridViewTextBoxColumn txtStartDate = new DataGridViewTextBoxColumn();
            txtStartDate.Name = "StartDate";
            txtStartDate.DataPropertyName = "StartDate";
            txtStartDate.HeaderText = Properties.Resources.ColumnHeader_StartDate;
            GRD.Columns.Add(txtStartDate);

            // EndDate
            DataGridViewTextBoxColumn txtEndDate = new DataGridViewTextBoxColumn();
            txtEndDate.Name = "EndDate";
            txtEndDate.DataPropertyName = "EndDate";
            txtEndDate.HeaderText = Properties.Resources.ColumnHeader_EndDate;
            GRD.Columns.Add(txtEndDate);

            // Duration
            DataGridViewDurationTextBoxColumn colDuration = new DataGridViewDurationTextBoxColumn();
            colDuration.Name = "Duration";
            colDuration.DataPropertyName = "Duration";
            colDuration.HeaderText = Properties.Resources.ColumnHeader_Duration;
            colDuration.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;            
            GRD.Columns.Add(colDuration);            

            // EarliestStartDate
            DataGridViewTextBoxColumn txtEarliestStartDate = new DataGridViewTextBoxColumn();
            txtEarliestStartDate.Name = "EarliestStartDate";
            txtEarliestStartDate.DataPropertyName = "EarliestStartDate";
            txtEarliestStartDate.HeaderText = Properties.Resources.ColumnHeader_EarliestStartDate;
            txtEarliestStartDate.Width = 110;
            txtEarliestStartDate.DefaultCellStyle.DataSourceNullValue = DateTime.MinValue;
            GRD.Columns.Add(txtEarliestStartDate);

            // LatestStartDate
            DataGridViewTextBoxColumn txtLatestStartDate = new DataGridViewTextBoxColumn();
            txtLatestStartDate.Name = "LatestStartDate";
            txtLatestStartDate.DataPropertyName = "LatestStartDate";
            txtLatestStartDate.HeaderText = Properties.Resources.ColumnHeader_LatestStartDate;
            txtLatestStartDate.DefaultCellStyle.DataSourceNullValue = DateTime.MaxValue;
            txtLatestStartDate.Width = 110;
            txtLatestStartDate.DefaultCellStyle.DataSourceNullValue = DateTime.MaxValue;
            GRD.Columns.Add(txtLatestStartDate);

            // EarliestEndDate
            DataGridViewTextBoxColumn txtEarliestEndDate = new DataGridViewTextBoxColumn();
            txtEarliestEndDate.Name = "EarliestEndDate";
            txtEarliestEndDate.DataPropertyName = "EarliestEndDate";
            txtEarliestEndDate.HeaderText = Properties.Resources.ColumnHeader_EarliestEndDate;
            txtEarliestEndDate.Width = 110;
            txtEarliestEndDate.DefaultCellStyle.DataSourceNullValue = DateTime.MinValue;
            GRD.Columns.Add(txtEarliestEndDate);

            // LatestEndDate
            DataGridViewTextBoxColumn txtLatestEndDate = new DataGridViewTextBoxColumn();
            txtLatestEndDate.Name = "LatestEndDate";
            txtLatestEndDate.DataPropertyName = "LatestEndDate";
            txtLatestEndDate.HeaderText = Properties.Resources.ColumnHeader_LatestEndDate;
            txtLatestEndDate.DefaultCellStyle.DataSourceNullValue = DateTime.MaxValue;
            txtLatestEndDate.Width = 110;
            txtLatestEndDate.DefaultCellStyle.DataSourceNullValue = DateTime.MaxValue;
            GRD.Columns.Add(txtLatestEndDate);

            // StartDateRule            
            DataGridViewComboBoxColumn colStartDateRule = new DataGridViewComboBoxColumn();
            colStartDateRule.Name = "StartDateRule";
            KS.Gantt.Dialogs.ComboBoxHelpers.FillComboEx(colStartDateRule, typeof(KS.Gantt.DateRules));
            colStartDateRule.DataPropertyName = "StartDateRule";
            colStartDateRule.HeaderText = Properties.Resources.ColumnHeader_StartDateRule;
            colStartDateRule.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            GRD.Columns.Add(colStartDateRule);

            // EndDateRule            
            DataGridViewComboBoxColumn colEndDateRule = new DataGridViewComboBoxColumn();
            colEndDateRule.Name = "EndDateRule";
            KS.Gantt.Dialogs.ComboBoxHelpers.FillComboEx(colEndDateRule, typeof(KS.Gantt.DateRules));
            colEndDateRule.DataPropertyName = "EndDateRule";
            colEndDateRule.HeaderText = Properties.Resources.ColumnHeader_EndDateRule;
            colEndDateRule.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            GRD.Columns.Add(colEndDateRule);

            // Description
            DataGridViewTextBoxColumn txtDescription = new DataGridViewTextBoxColumn();
            txtDescription.Name = "Description";
            txtDescription.DataPropertyName = "Description";
            txtDescription.HeaderText = Properties.Resources.ColumnHeader_Description;
            txtDescription.Width = 240;
            GRD.Columns.Add(txtDescription);

            EnableControls();            
        }

        protected override void mnuNew_Click(object sender, EventArgs e)
        {
            base.mnuNew_Click(sender, e);

            if (m_GanttControl == null)
                return;

            Program.CursorWait();
            KS.Gantt.Dialogs.TaskEditDialog Dlg = new KS.Gantt.Dialogs.TaskEditDialog();
            Dlg.Edit(m_GanttControl, null);
            Dlg.ShowDialog(this);
            Dlg.Dispose();
            Dlg = null;
        }

        protected override void mnuEdit_Click(object sender, EventArgs e)
        {
            base.mnuEdit_Click(sender, e);

            if (m_GanttControl == null || GRD.CurrentRow == null)
                return;

            GanttItem item = (GanttItem)GRD.CurrentRow.DataBoundItem;
            Program.CursorWait();
            m_GanttControl.EditItem(item);

            SelectGanttItem(item);
        }
    }
}
